package com.google.code;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ThermeratorsActivity extends Activity {
    /** Called when the activity is first created. */
	
	public static List<String> deviceNameList;
	public static ArrayAdapter<String> deviceArray;
	public static FileHandler file;
	public static String[] deviceInfo;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ListView deviceList = (ListView)findViewById(R.id.devicelist);
        
        file = new FileHandler(getExternalFilesDir(null),"therm.txt");
        
        deviceNameList = new ArrayList<String>();
        deviceNameList.add("Getting list of availbale devices.");
        
        deviceArray = new ArrayAdapter<String>(this,R.layout.devicelistlayout,R.id.textDeviceName,deviceNameList);
        deviceList.setAdapter(deviceArray);
        deviceList.setOnItemClickListener(new OnItemClickListener() 
        {
        	public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
        	{
        		String ip = deviceInfo[(int) ((id*2)+1)];
        		Intent i = new Intent();
        		i.putExtra("IP_ADDRESS", ip);
        		i.setClassName("com.google.code", "com.google.code.mainScreen");
        		startActivity(i);
        	}
        });
        
        getDeviceList();
        deviceArray.notifyDataSetChanged();
    }
    
    public static void getDeviceList()
    {
    	file.writeFile("Device1,192.168.1.123");
    	String contents = file.getContents();
    	deviceInfo = contents.split(",");
    	deviceNameList.clear();
    	for(int i = 0;i < deviceInfo.length;i+=2)
    		deviceNameList.add(deviceInfo[i]);
    }
}